<?php
namespace app\shop\model;
use app\rbac\model\AdminM;
use think\Validate;
use think\Db;
use app\base\model\Base;
class AttributeM extends Base
{

       protected function _V($scene,$data)
       {
           $rule = [
               'attr_name' => 'require',
               'attr_type' => 'require',
               'type_id' => 'require|number',
           ];
        $msg = [
        'attr_name.require' => '属性名称不能为空',
        'attr_type.require' => '属性类型不能为空',
        'type_id.require' => '类型不能为空',
        'type_id.number' => '类型只能是数字',

        ];
           $validate = new Validate($rule,$msg);
           $validate->scene('add', ['attr_name','attr_type','type_id']);
           $validate->scene('edit', ['attr_name','attr_type','type_id']);
           $result = $validate->scene($scene)->check($data);
           if($result === false)
           {
               return $validate->getError();
           }
           else
           {
               return true;
           }

       }

    /**
     * 新增数据
     * @return mixed
     */

    public function add($data)
    {
        //验证数据
        $res = $this->_V('add',$data);
        if($res !== true)
        {
            $this->code = -1;
            $this->msg = $res;
            return false;
        }
        $insertData = [];
        //判断用户
        $insertData['admin_id'] = session('admin_id');
        $insertData['attr_name'] = $data['attr_name'];
        $insertData['attr_type'] = $data['attr_type'];
        $insertData['type_id'] = $data['type_id'];
        //
        $insertData['attr_option_values'] = trim(str_replace('，',',',addslashes(strip_tags($data['attr_option_values']))),',');
        $id = Db::table('shop_attribute')->insertGetId($insertData);
        if(!$id)
        {
            $this->code = -1;
            $this->msg = '新增失败';
            return false;
        }
        return true;
    }

    /**
     * 全部数据
     */
    public function lst($page = 15,$type_id)
    {
        $data = Db::table('shop_attribute')
            ->alias('a')
            ->field('a.id,a.attr_name,a.attr_type,attr_option_values,a.type_id,t.name as type_name')
            ->join('shop_type t','t.id=a.type_id','LEFT')
            ->where('type_id',$type_id)
            ->paginate($page);
        return $data;
    }

    /**
     * 修改数据
     * @param $data
     * @return bool
     */
    public function edit($data)
    {
        $res = $this->_V('edit',$data);
        if($res !== true)
        {
            $this->code = -1;
            $this->msg = $res;
            return false;
        }
        $insertData = [];
        //判断用户
        $insertData['admin_id'] = session('admin_id');
        $insertData['attr_name'] = $data['attr_name'];
        $insertData['attr_type'] = $data['attr_type'];
        $insertData['type_id'] = $data['type_id'];
        //
        $insertData['attr_option_values'] = trim(str_replace('，',',',addslashes(strip_tags($data['attr_option_values']))),',');

        $id = Db::table('shop_attribute')->where('id',$data['id'])->update($insertData);
        if(!$id)
        {
            $this->code = -1;
            $this->msg = '修改失败';
            return false;
        }
        return true;
    }

    /**
     * 删除数据
     * @param $id
     */
    public function del($id)
    {
        $res = Db::table('shop_goods_attr')->where('attr_id',$id)->field('id')->find();
        if(!empty($res))
        {
            $this->code = -1;
            $this->msg = '有商品使用此属性，无法删除';
            return false;
        }
        return Db::table('shop_attribute')->where('id',$id)->delete();
    }

    /**
     * 获取商品属性
     * @param $goods_id
     * @return mixed
     */
    public function getGoodsAttr($goods_id){
        return Db::table('shop_attribute')
            ->alias('a')
            ->field('a.id attr_id,a.attr_name,a.attr_type,a.attr_option_values,a.type_id,b.attr_value,b.id')
            ->join('shop_goods_attr b', 'b.attr_id=a.id','LEFT')
            ->where(['b.goods_id'=>$goods_id])
            ->select();
    }
}
